import { Column, Entity, Index, OneToMany } from 'typeorm';
import { CommonEntity } from '~/common/entity/common.entity';
import { MissionEntity } from '~/modules/mission/mission.entity';

export enum PointRuleType {
  FIXED = 'FIXED', // 固定积分
  MULTIPLIER = 'MULTIPLIER', // 倍率积分
  TIERED = 'TIERED', // 阶梯积分
  TIME_BASED = 'TIME-BASED', // 时间加成积分
  RANDOM = 'RANDOM', // 随机积分
}

export enum PointRuleRewardType {
  PER_ATTEMPT = 'PER_ATTEMPT', // 每次完成都发放积分
  FULL_COMPLETION = 'FULL_COMPLETION', // 任务全部完成后才发放积分
}


@Entity('ah_points_rules')
@Index(['id'])
export class PointsRulesEntity extends CommonEntity {
  @Column({ name: 'rule_name', type: 'varchar', length: 255, nullable: false, comment: '积分规则名称' })
  ruleName: string;

  @Column({ name: 'description', type: 'text', nullable: true, comment: '积分规则描述' })
  description: string;

  @Column({ name: 'rule_type', type: 'enum', enum: PointRuleType, default: PointRuleType.FIXED, comment: '积分规则类型' })
  ruleType: PointRuleType;

	@Column({ name: 'reward_type', type: 'enum', enum: PointRuleRewardType, default: PointRuleRewardType.PER_ATTEMPT, comment: '积分发放类型' })
	rewardType: PointRuleRewardType

	@Column({ nullable: false, type: 'int', comment: '需要完成的次数', name: 'target_count' })
  targetCount: number;

  @Column({ type: 'int', nullable: true, default: null, name: 'fixed_points', comment: '固定积分值' })
  fixedPoints: number; // 固定积分值（仅适用于 fixed 规则）

  @Column({ type: 'float', nullable: true, default: null, comment: '倍率（仅适用于 multiplier 规则）' })
  multiplier: number; // 倍率（仅适用于 multiplier 规则）

  @Column({ type: 'longtext', nullable: true, default: null, name: 'tiered_points', comment: '阶梯积分，如 `{ "50%": 30, "100%": 100 }`' })
  tieredPoints: string; // 阶梯积分，如 `{ "50%": 30, "100%": 100 }`

  @Column({ type: 'longtext', nullable: true, default: null, name: 'time_bonus', comment: '时间加成规则，如 `{ "6:00-9:00": "double", "18:00-21:00": 2 }`' })
  timeBonus: string; // 时间加成规则，如 `{ "6:00-9:00": "double", "18:00-21:00": 2 }`

  @Column({ type: 'int', nullable: true, default: null, name: 'random_points', comment: '随机积分最小值' })
  randomMin: number; // 随机积分最小值

  @Column({ type: 'int', nullable: true, default: null, name: 'random_max', comment: '随机积分最大值' })
  randomMax: number; // 随机积分最大值

	@OneToMany(() => MissionEntity, (mission) => mission.pointsRule, { cascade: true })
	missions: MissionEntity[]; // 关联的任务列表
}
